{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cc2904b5",
   "metadata": {},
   "source": [
    "## 4.1 if语句 if/if-elif语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31b4a07f",
   "metadata": {},
   "source": [
    "if-else 语句，只有两种情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e5f5de43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "x=15\n",
    "if x!=10:\n",
    "    print(\"hello\")\n",
    "else:\n",
    "    print(\"world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7456bc89",
   "metadata": {},
   "source": [
    "if-elif-else 语句，可以设置多种情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a626478",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "x=10\n",
    "y=15\n",
    "if x<y:\n",
    "    print(\"1\")\n",
    "elif x>y:\n",
    "    print(\"2\")\n",
    "else:\n",
    "    print(\"3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d686cb0",
   "metadata": {},
   "source": [
    "当然，if语句可以进行嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7ccda025",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x<y\n",
      "x=10\n"
     ]
    }
   ],
   "source": [
    "x=10\n",
    "y=15\n",
    "if x>y:\n",
    "    print(\"x>y\")\n",
    "elif x<y:\n",
    "    print(\"x<y\")\n",
    "    if x==10:\n",
    "        print(\"x=10\")\n",
    "    else:\n",
    "        print(\" \")\n",
    "else:\n",
    "    print(\"x=y\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd339cff",
   "metadata": {},
   "source": [
    "## 4.2 循环语句 (for while语句)\n",
    "\n",
    "### 1.for 语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce994641",
   "metadata": {},
   "source": [
    "for 定义数值列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0b970f73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 3] <class 'list'>\n",
      "[4, 5, 6] <class 'list'>\n",
      "[7, 8, 9] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "#直接划定range\n",
    "dig=list()\n",
    "for i in range(1,6):\n",
    "    dig.append(i)\n",
    "print(dig)\n",
    "\n",
    "#每次list1得到的值也是一个列表\n",
    "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "for list1 in list_of_lists:\n",
    "    print(list1,type(list1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c539be7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "385\n",
      "[1, 5, 14, 30, 55, 91, 140, 204, 285, 385]\n",
      "385\n"
     ]
    }
   ],
   "source": [
    "sum1=0 #先定义一个数sum\n",
    "numbers=[]\n",
    "for number in range(1,11):\n",
    "    square=number**2\n",
    "    sum1=sum1+square\n",
    "    numbers.append(sum1)\n",
    "print(sum1)#输出1~10的平方和\n",
    "print(numbers)\n",
    "print(max(numbers))#统计计算，一般用到的有max min sum"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e62c68c",
   "metadata": {},
   "source": [
    "for也可以写在定义行里面，这样可以直接一行生成列表，简便"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f27fcc91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n",
      "\t\n"
     ]
    }
   ],
   "source": [
    "numbers=[number**2 for number in range(1,11)]#for语句没有冒号\n",
    "print(numbers)\n",
    "#解析的写法不适用于要用到上一个循环值的计算！！（没办法在括号里写等式）但可以简化代码！\n",
    "print(\"\\t\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "052179c1",
   "metadata": {},
   "source": [
    "也可以同时使用两个数的循环 \n",
    "#### 注意两个for语句是嵌套关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bfe6af5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1,\n",
       " 2,\n",
       " 3,\n",
       " 4,\n",
       " 5,\n",
       " 6,\n",
       " 2,\n",
       " 3,\n",
       " 4,\n",
       " 5,\n",
       " 6,\n",
       " 7,\n",
       " 4,\n",
       " 5,\n",
       " 6,\n",
       " 7,\n",
       " 8,\n",
       " 9,\n",
       " 8,\n",
       " 9,\n",
       " 10,\n",
       " 11,\n",
       " 12,\n",
       " 13,\n",
       " 16,\n",
       " 17,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 21]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[2**i+z for i in range(5)  for z in range(6)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dacafc0",
   "metadata": {},
   "source": [
    "还可以嵌套if语句，类似的方法也可以用来定义词典、集合、元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b57236c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'1': 27, '2': 729, '3': 19683, '4': 531441, '5': 14348907, '6': 387420489, '7': 10460353203, '8': 282429536481, '9': 7625597484987, '10': 205891132094649}\n",
      "(27, 729, 19683, 531441, 14348907, 387420489, 10460353203, 282429536481, 7625597484987, 205891132094649)\n"
     ]
    }
   ],
   "source": [
    "dic={str(i):27**i for i in range(1,20) if i<=10}\n",
    "print(dic)\n",
    "\n",
    "print(tuple(27**x for x in range(1,20) if x<=10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "194d2984",
   "metadata": {},
   "source": [
    "for循环的嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "78bb3e82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1   <class 'int'>\n",
      "2   <class 'int'>\n",
      "3   <class 'int'>\n",
      "\t\n",
      "4   <class 'int'>\n",
      "5   <class 'int'>\n",
      "6   <class 'int'>\n",
      "\t\n",
      "7   <class 'int'>\n",
      "8   <class 'int'>\n",
      "9   <class 'int'>\n",
      "\t\n"
     ]
    }
   ],
   "source": [
    "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "for list in list_of_lists:\n",
    "    for list1 in list:\n",
    "        print(list1,\" \",type(list1))\n",
    "    print(\"\\t\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "553d632d",
   "metadata": {},
   "source": [
    "### 2.while语句（continue and break）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "05814dee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "Bye\n",
      "looping   4\n",
      "looping   5\n",
      "looping   6\n",
      "looping   7\n",
      "looping   8\n",
      "looping   9\n",
      "looping  10\n",
      "looping  11\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "while i < 3:\n",
    "    print(i ** 2)\n",
    "    i = i+1\n",
    "print('Bye')\n",
    "\n",
    "# do-untile\n",
    "while True:\n",
    "    #do something\n",
    "    i = i+1\n",
    "    print('looping %3d' % i)\n",
    "    \n",
    "    # check \n",
    "    if i>10: break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5cd09ea",
   "metadata": {},
   "source": [
    "break 就是在循环中的某个条件为真时，终止循环\n",
    "\n",
    "continue 则是避免循环的终止"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11777fee",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    if i>4:\n",
    "        print(\"The end.\")\n",
    "        continue\n",
    "#如果没有continue，那么就会进入i<7的循环，而这时要持续输出the end\n",
    "    elif i<7:\n",
    "        print(i)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
